package com.wewoo.log4e.starter.executor;

import com.wewoo.log4e.core.entity.OperationLog;
import com.wewoo.log4e.core.executor.AbstractLogExecutor;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @ClassName DataSourceExecutor
 * @Description 操作记录 数据库执行器
 * @Author tangjin
 * @Date 16:30 2022/2/14
 **/
public class DataSourceExecutor extends AbstractLogExecutor<OperationLog> {
    private Logger logger = LoggerFactory.getLogger(DataSourceExecutor.class);
    private final JdbcTemplate jdbcTemplate;

    public DataSourceExecutor(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public OperationLog writeLog(OperationLog log) {
        logger.info("执行一个操作记录的入库");
        String sql = "insert into operation_log" +
                "(id" +
                ",relate_unique" +
                ",level" +
                ",operation_unit" +
                ",operation_type" +
                ",handle_type" +
                ",method" +
                ",args" +
                ",user_id" +
                ",user_name" +
                ",descripition" +
                ",run_time" +
                ",return_value" +
                ",create_time)" +
                " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                log.getId(),
                log.getRelateUnique(),
                log.getLevel(),
                log.getOperationUnit(),
                log.getOperationType(),
                log.getHandleType(),
                log.getMethod(),
                log.getArgs(),
                log.getUserId(),
                log.getUserName(),
                log.getDescripition(),
                log.getRunTime(),
                log.getReturnValue(),
                log.getCreateTime());
        return log;
    }

}
